package com.api.gbhg.liuwenbo.version3.api;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.api.gbhg.liuwenbo.version3.pojo.BudgetSectionIncome3;
import com.api.gbhg.liuwenbo.version3.util.ApiUtil;
import com.customization.assets.util.DateTools;
import com.customization.commons.LogTool;
import weaver.conn.RecordSet;
import weaver.general.GCONST;

import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * @Description lwb
 * @Author 财政拨款收入预算统计表-经费科报表
 * @Veresion 1.0
 * @Date 2023.02.03
 */
@Path("v3/FinancialInStat")
public class FinancialInStatApi {
    private static LogTool logTool = new LogTool("/log/gbhg/report/FinancialInStatApi",false);
    private DateTools dateTools = new DateTools();

    public static void main(String[] args) {
        String rootPath = "D:\\WEAVER\\ecology\\";
        GCONST.setRootPath(rootPath);
        GCONST.setServerName("ecology");

        FinancialInStatApi api = new FinancialInStatApi();
        Map param = new HashMap();
        param.put("nf","2023");
        param.put("shrq","2023-10-30");
        System.err.println(api.getFinancialInStatList(param));
    }
    @POST
    @Path("/getFinancialInStatList")
    @Produces(MediaType.APPLICATION_JSON)
    public String getFinancialInStatList(Map param){
        Map msgMap = new HashMap();
        logTool.writeLog("接收参数："+ JSONObject.toJSONString(param));
        if (param == null ||!param.containsKey("nf")){
            msgMap.put("code","400");
            msgMap.put("msg","缺少参数 请检查参数是否完整！");
            msgMap.put("data","");
            return JSON.toJSONString(msgMap);
        }
        //2023年4月11日 huangwm 修复执行率为0的bug

        try {
            String shrq = "";
            if(param.containsKey("shrq")){
                if(param.get("shrq").toString().isEmpty()){
                    shrq = dateTools.getCurrentDate();
                }else{
                    shrq = param.get("shrq").toString();
                }
            }
            if(!param.containsKey("shrq")){
                shrq = dateTools.getCurrentDate();
            }
            List<BudgetSectionIncome3> listData = getDataKm(param.get("nf").toString(),shrq);

            msgMap.put("code",200);
            msgMap.put("msg","success");
            msgMap.put("data",listData);
        } catch (Exception e) {
            e.printStackTrace();
            msgMap.put("code",500);
            msgMap.put("msg",e.toString());
            msgMap.put("data","");
        }

        return JSON.toJSONString(msgMap);
    }

    /**
     * 获取数据
     * @param nf
     * @return
     */
    public List<BudgetSectionIncome3> getDataKm(String nf,String shrq){
        RecordSet rs = new RecordSet();
        String sql = "select * from uf_zyczbksr_km where dw = 1 and srnf = "+nf;
        rs.executeQuery(sql);
        List<BudgetSectionIncome3> list = new LinkedList<>();
        while (rs.next()){
            String kmdm = rs.getString("kmdm");
            //由于kmdm库内带有kmmc 所以kmdm需要处理
            List<BudgetSectionIncome3> data = new LinkedList<>();
            if (rs.getString("gnkmmc").startsWith("1")){
                //基本支出
                data = getDataEj(rs.getString("id"), rs.getString("gnkmmc"), kmdm.substring(0, kmdm.length() - 8), nf,1,shrq);
                if (data.isEmpty()){
                    //没有下级 取本级
                    BudgetSectionIncome3 bean = new BudgetSectionIncome3();
                    //没有下级 只有科目级
                    bean.setKmjxj(0);
                    bean.setId(rs.getString("id"));
                    bean.setCategory("基本支出");
                    bean.setKmmc(rs.getString("gnkmmc"));
                    bean.setName(kmdm.substring(0, kmdm.length() - 8));
                    bean.setYqndsjjz(rs.getString("yqndsjjzje"));
                    bean.setYjsr(rs.getString("yjsr"));
                    String snhp = rs.getString("snjzhpje");
                    String bnhp = rs.getString("yhpje");
                    //改为getZycz_tybxje方法 huangwm 2023年9月21日
                    bean.setSnzc(ApiUtil.getZycz_tybxje("0","1","0",rs.getString("id"),"1",nf,shrq));
                    bean.setBnzc(ApiUtil.getZycz_tybxje("0","0","0",rs.getString("id"),"1",nf,shrq));
                    bean.setSnyssy(new BigDecimal(snhp).subtract(new BigDecimal(bean.getSnzc())).toString());
                    bean.setBnyssy(new BigDecimal(bnhp).subtract(new BigDecimal(bean.getBnzc())).toString());
                    BigDecimal hpze = new BigDecimal(snhp).add(new BigDecimal(bnhp));//核批总额
                    bean.setHpze(hpze.toString());
                    BigDecimal zcze = new BigDecimal(bean.getSnzc()).add(new BigDecimal(bean.getBnzc()));//支出总额
                    bean.setZcze(zcze.toString());
                    BigDecimal bai = new BigDecimal("100.00");
                    if (hpze.compareTo(BigDecimal.ZERO)>0 && zcze.compareTo(BigDecimal.ZERO)>0){
                        //bean.setZxl(zcze.divide(hpze).toString()+"%");
                        bean.setZxl(zcze.divide(hpze,4,BigDecimal.ROUND_HALF_UP).multiply(bai).setScale(2).toString()+"%");
                    }else{
                        bean.setZxl("0.00%");
                    }
                    list.add(bean);
                }else {
                    //有下级
                    list.addAll(data);
                }
            }else{
                //项目支出
                data = getDataEj(rs.getString("id"), rs.getString("gnkmmc"), kmdm.substring(0, kmdm.length() - 8), nf,2,shrq);
                if (data.isEmpty()){
                    //没有下级 只有科目级
                    BudgetSectionIncome3 bean = new BudgetSectionIncome3();
                    //没有下级 只有科目级
                    bean.setKmjxj(0);
                    bean.setId(rs.getString("id"));
                    bean.setCategory("基本支出");
                    bean.setKmmc(rs.getString("gnkmmc"));
                    bean.setName(kmdm.substring(0, kmdm.length() - 8));
                    bean.setYqndsjjz(rs.getString("yqndsjjzje"));
                    bean.setYjsr(rs.getString("yjsr"));
                    String snhp = rs.getString("snjzhpje");
                    String bnhp = rs.getString("yhpje");
                    //改为getZycz_tybxje方法 huangwm 2023年9月21日
                    bean.setSnzc(ApiUtil.getZycz_tybxje("0","1","0",rs.getString("id"),"1",nf,shrq));
                    bean.setBnzc(ApiUtil.getZycz_tybxje("0","0","0",rs.getString("id"),"1",nf,shrq));
                    bean.setSnyssy(new BigDecimal(snhp).subtract(new BigDecimal(bean.getSnzc())).toString());
                    bean.setBnyssy(new BigDecimal(bnhp).subtract(new BigDecimal(bean.getBnzc())).toString());
                    BigDecimal hpze = new BigDecimal(snhp).add(new BigDecimal(bnhp));//核批总额
                    bean.setHpze(hpze.toString());
                    BigDecimal zcze = new BigDecimal(bean.getSnzc()).add(new BigDecimal(bean.getBnzc()));//支出总额
                    bean.setZcze(zcze.toString());
                    BigDecimal bai = new BigDecimal("100.00");
                    if (hpze.compareTo(BigDecimal.ZERO)>0 && zcze.compareTo(BigDecimal.ZERO)>0){
                        //bean.setZxl(zcze.divide(hpze).toString()+"%");
                        bean.setZxl(zcze.divide(hpze,4,BigDecimal.ROUND_HALF_UP).multiply(bai).setScale(2).toString()+"%");
                    }else{
                        bean.setZxl("0.00%");
                    }
                    list.add(bean);
                }else {
                    list.addAll(data);
                }
            }

        }
        return list;
    }

    /**
     * 获取二级数据 实际跟下级数据同表只不过查二级条件不同
     * @param id
     * @return
     */
    public List<BudgetSectionIncome3> getDataEj(String id,String kmmc,String name,String nf,int number,String shrq){
        RecordSet rs = new RecordSet();
        String sql = "select * from uf_zyczbksr_xj where szgnkm = "+id+" and sjzxsr is null ";
        rs.executeQuery(sql);
        List<BudgetSectionIncome3> list = new LinkedList<>();
        while (rs.next()){
            if (isXj(rs.getString("id"))){
                //有三级
                list.addAll(getDataXj(rs.getString("id"),kmmc,name,rs.getString("srzx"),nf,number,shrq));

            }else{
                //只有二级
                BudgetSectionIncome3 bean = new BudgetSectionIncome3();
                bean.setKmjxj(1);
                bean.setId(rs.getString("id"));
                if (number==1){
                    bean.setCategory("基本支出");
                }else{
                    bean.setCategory("项目支出");
                    //项目支出 需要取值项目代码
                    //bean.setXmdm(rs.getString("xmdm")+rs.getString("glh")); 20230921取消关联号的追加显示
                    bean.setXmdm(rs.getString("xmdm"));
                }
                bean.setKmmc(kmmc);
                bean.setName(name);
                bean.setXmmc(rs.getString("srzx"));
                bean.setYqndsjjz(rs.getString("yqndsjjzje"));
                bean.setYjsr(rs.getString("yjsr"));
                String snhp = rs.getString("snjzhpje");
                String bnhp = rs.getString("yhpje");
                //改为getZycz_tybxje方法 huangwm 2023年9月21日
                bean.setSnzc(ApiUtil.getZycz_tybxje("0","1","1",rs.getString("id"),"1",nf,shrq));
                bean.setBnzc(ApiUtil.getZycz_tybxje("0","0","1",rs.getString("id"),"1",nf,shrq));
                bean.setSnyssy(new BigDecimal(snhp).subtract(new BigDecimal(bean.getSnzc())).toString());
                bean.setBnyssy(new BigDecimal(bnhp).subtract(new BigDecimal(bean.getBnzc())).toString());
                BigDecimal hpze = new BigDecimal(snhp).add(new BigDecimal(bnhp));//核批总额
                bean.setHpze(hpze.toString());
                BigDecimal zcze = new BigDecimal(bean.getSnzc()).add(new BigDecimal(bean.getBnzc()));//支出总额
                bean.setZcze(zcze.toString());
                BigDecimal bai = new BigDecimal("100.00");

                if (hpze.compareTo(BigDecimal.ZERO)>0 && zcze.compareTo(BigDecimal.ZERO)>0){
                    //bean.setZxl(zcze.divide(hpze).toString()+"%");
                    //huangwm 2023年8月29日 修复执行率显示不正确 .multiply(bai)
                    bean.setZxl(zcze.divide(hpze,4,BigDecimal.ROUND_HALF_UP).multiply(bai).setScale(2).toString()+"%");
                }else{
                    bean.setZxl("0.00%");
                }
                list.add(bean);
            }
        }
        return list;
    }

    /**
     * 获取下级数据
     * @param id
     * @return
     */
    public List<BudgetSectionIncome3> getDataXj(String id,String kmmc,String name,String xmmc,String nf,int number,String shrq){
        RecordSet rs = new RecordSet();
        String sql = "select * from uf_zyczbksr_xj where sjzxsr = "+id+" ";
        rs.executeQuery(sql);

        List<BudgetSectionIncome3> list = new LinkedList<>();
        while (rs.next()){
            BudgetSectionIncome3 bean = new BudgetSectionIncome3();
            bean.setKmjxj(1);
            bean.setId(rs.getString("id"));
            if (number==1){
                bean.setCategory("基本支出");
            }else{
                bean.setCategory("项目支出");
                //项目支出 需要取值项目代码
                bean.setXmdm(rs.getString("xmdm")+rs.getString("glh"));
            }
            bean.setKmmc(kmmc);
            bean.setName(name);
            bean.setXmmc(xmmc+"-"+rs.getString("srzx"));
            bean.setYqndsjjz(rs.getString("yqndsjjzje"));
            bean.setYjsr(rs.getString("yjsr"));
            String snhp = rs.getString("snjzhpje");
            String bnhp = rs.getString("yhpje");
            //改为getZycz_tybxje方法 huangwm 2023年9月21日
            bean.setSnzc(ApiUtil.getZycz_tybxje("0","1","1",rs.getString("id"),"1",nf,shrq));
            bean.setBnzc(ApiUtil.getZycz_tybxje("0","0","1",rs.getString("id"),"1",nf,shrq));
            bean.setSnyssy(new BigDecimal(snhp).subtract(new BigDecimal(bean.getSnzc())).toString());
            bean.setBnyssy(new BigDecimal(bnhp).subtract(new BigDecimal(bean.getBnzc())).toString());
            BigDecimal hpze = new BigDecimal(snhp).add(new BigDecimal(bnhp));//核批总额
            bean.setHpze(hpze.toString());
            BigDecimal zcze = new BigDecimal(bean.getSnzc()).add(new BigDecimal(bean.getBnzc()));//支出总额
            bean.setZcze(zcze.toString());
            BigDecimal bai = new BigDecimal("100.00");
            if (hpze.compareTo(BigDecimal.ZERO)>0 && zcze.compareTo(BigDecimal.ZERO)>0){
                //bean.setZxl(zcze.divide(hpze).toString()+"%");
                bean.setZxl(zcze.divide(hpze,4,BigDecimal.ROUND_HALF_UP).multiply(bai).setScale(2).toString()+"%");
            }else{
                bean.setZxl("0.00%");
            }
            list.add(bean);
        }
        return list;
    }

    public boolean isXj(String id){
        RecordSet rs = new RecordSet();
        String sql = "select id from uf_zyczbksr_xj where sjzxsr = "+id+" ";
        rs.executeQuery(sql);
        return rs.next();
    }
}
